// pages/hb_sjh/hb_sjh.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    count:60,
    code:'获取验证码',
    iphoneValue:' ', //手机号码
    yz:'0'
  },
  sm_rz:function(e){
    wx.request({
      url: app.globalData.url_l+'api/member/mobile_bang',
      method: 'post',
      data:{
        mobile:app.globalData.shouji1,
        yanzhengma:app.globalData.yanz,
        uid:app.globalData.uid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if(res.data.code==2){
          wx.showToast({
            title:"该手机号已经绑定过了。",
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showLoading({
            title: '修改完成',
            mask:true
          })
          
          setTimeout(function () {
            wx.navigateBack()
          }, 1000)
          
        }
        
      }
    })
    
  },
   // 验证码按钮
   yanzheng1:function(e){
    let that = this
  // 手机号码格式验证
  this.data.yz++
  if (!(/^1[3456789]\d{9}$/.test(app.globalData.shouji1))) {
    this.data.yz=0
    wx.showToast({
      title: '输入手机号有误',
      icon: 'none',
      duration: 2000
    })
    return;
  }
  if(that.data.code !== '获取验证码'){
    return
  }
  if(this.data.yz==1){
    const countDown = setInterval(() => {
      if(that.data.count <= 0){
        that.setData({
          count:60,
          code:'获取验证码'
        })
        clearInterval(countDown)
          return
        }
        that.data.count --
        if(that.data.count==0){
          this.data.yz=0
        }
        that.setData({
          count: that.data.count,
          code: that.data.count < 10 ? `请等待0${that.data.count}s` : `请等待${that.data.count}s`
        })
      },1000);
  }
 
  // 调用验证码接口
      wx.request({
        url: app.globalData.url_l + 'api/index/yanzhengma',
        method: 'post',
        data:{
          mobile:app.globalData.shouji1,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          })
        
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  shouji:function(e){
    app.globalData.shouji1 = e.detail.value
    console.log(app.globalData.shouji1)
  },
  yanzheng:function(e){
    app.globalData.yanz = e.detail.value
    console.log(app.globalData.yanz)
  },
  onLoad: function (options) {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})